package cc.eddic.pratice.programmingparadigms.emp02list.impl;

import cc.eddic.pratice.programmingparadigms.Employee;
import cc.eddic.pratice.programmingparadigms.emp02list.EmployeeList;


public class EmployeeListWqjImpl implements EmployeeList {

    private Employee[] employees;
    private int index;

    public EmployeeListWqjImpl(int capacity) {
        employees = new Employee[capacity];
        index = 0;
    }

    @Override
    public void add(Employee employee) {
        if(index + 1 >= employees.length){
            Employee[] newEmployee = new Employee[employees.length*2];
            System.arraycopy(employees, 0, newEmployee, 0, index + 1);
            employees = newEmployee;
        }
        employees[index] = employee;
        index++;

    }
    @Override
    public Employee get(int index) {
        if (index > size() - 1 || size() == 0){
            throw new IndexOutOfBoundsException("Index: " + index +", Size: " + size());
        }
        return employees[index];
    }

    @Override
    public int size() {
        int count = 0;
        for (int i = 0; i < index; i++){
            if (null != employees[i]){
                count ++;
            } else {
                break;
            }
        }
        return count;
    }

}